<?php


/**
 * CF_class_com.php includes main auto creation classes
 * This package licenced under GPL
 * @package CFtable
 * @license http://www.gnu.org/licenses/gpl-3.0-standalone.html
 * @copyright Managed Computer Solutions
 */
//  2008-12-19 changed 'showkey' and 'valuekey' to 'showvalue' and 'keyvalue' for drop down and multiple radio options




define("TABLE_EVEN_CLASS","even_row_color");
define("TABLE_ODD_CLASS","odd");



// ----------------------------- Field Spec settings are as below:	
//$fieldspec['<column>'] ['show'] = {'0' = notshown, '1' = show readonly, 'L' = show, use list (manual join), E = editable;
//$fieldspec['<column>'] ['key'] = {1 = key field, 0 of not (or dont include)};
//$fieldspec['<column>'] ['select'] = {'T' = text, 'D' = dropdown select, 'C' = checkbox (for 1 or 0 boolean), 'R' = radiogroup, 'A' = textarea, 'DT' or 'CAL' = calendar for date-time};
//$fieldspec['<column>'] ['filter'] = {1 if value is filtered for, 0 or undefined if not};	
//$fieldspec['<column>'] ['add'] = {1 = include value as input in add box, 0 or undefined = do not };
//$fieldspec['<column>'] ['size'] = size of input field, or columns (cols) for textarea;
//$fieldspec['<column>'] ['rows'] = rows of textarea or listbox (future);
//$fieldspec['<column>'] ['headname'] = {Name to show on column heading or input heading};
//$fieldspec['<column>'] ['keyvalue'] = {array column to use for key in drop-down lists} ;
//For lists, Not needed if columns 'keyvalue' and 'showvalue' are used in array, no need to define these specs;
//$fieldspec['<column>'] ['showvalue'] = {array column to use for shown value in drop-down lists};
//$fieldspec['<column>'] ['rawformat']	= additional html to include inside the input field;
//$fieldspec['<column>'] ['image']	= image file name of image to associate with control (implemetned for calendar);
//$fieldspec['<column>'] ['showdateformat'] = format string for inital display of shown date, using php date formatters;
//$fieldspec['<column>'] ['cal'] ['ifFormat'] = format string of date from database (or whatever) for input to calendar, js date formatters;
//$fieldspec['<column>'] ['cal'] ['align'] = {'T1' Shows calendar above imput control (default), 'B1' shows it below controls};
//$fieldspec['<column>'] ['cal'] ['showsTime'] = {true = show time in calendar below date, false (Default) = show and use calendar for dates only};
//$fieldspec['<column>'] ['cal'] ['singleClick'] = {true = (Default) open calendar with single click, false = double click for calendar};
//$fieldspec['<column>'] ['allowhtml'] = {true:  do not escape html (to allow images, etc), false (or unset by default) escapes using htmlspecialchars)};
//$fieldspec['<column>']['source'] = table name for source table, will be appended to filters to resolve ambiguies with multiple tables
 

include_once "CF_support.php";
// Classes
/**
 * BasicTable is a class to dumping of data from a database in an array.  This basic version makes an HTML output
* @package CFtable
 * @author Chris Tyler
 * @version 1.0
 * @BasicTable is the base class for the an extensable HTML table class for array based data.  This provides data display, sorting, filtering, paging and heading functions
 * @copyright 2007-2008 
 */
class BasicTable {
	/**
	* @var array $valuesarray A 2D array of values, an array of associative row arrays, valuesarray[<row number>][<column heading>]
	*/
	var $valuesarray;
	/**
	* @var array $fieldspec Array describes data and formatting options
	*/	
	var $fieldspec;
	/**
	* @var string $row_status Internal variable for determing odd or even row in table
	*/	
	var $row_status;
	/**
	* @var bool $show_all_rows Set to true to show all rows regardless of fieldspec in basic table
	*/
	var $show_all_rows;
	/**
	* @var string $table_odd_class Set the name of the class to give odd table rows
	*/	
	var $table_odd_class = 'odd_row_color';
	/**
	* @var string $table_even_class Set the name of the class to give odd table rows
	*/	
	var $table_even_class = 'even_row_color';	
	/**
	* @var array $keys array for keys as pulled from Request 
	*/  	
    var $keys;
	
	/**
	 *  BasicTable Constructor method will form table from spec and values
     * Default will show all columns
     * all checked rows at once.
     * @param array $the_spec  This is fieldspec array.  defaults to show all rows
     * @param array $values array.  This is 2d array by rows
	 */
	function BasicTable( $the_spec = 'all', $values_array = ''){
		$this->values_array = $values_array;
		if(is_array($the_spec)){
			$this->fieldspec = $the_spec;
			$this->show_all_rows = false;
			}
		else{
			$this->show_all_rows = true;
			}
	}
	
	/**
	 *  set_valuesarray Method will set values array to passed array
     * @param array $dataarray  This is array.  
	 */
	function set_valuesarray($dataarray){
		$this->valuesarray = $dataarray;	
	}	


	
/**
 * Set FieldSpec Function
 * @param array $thespec - The fieldspec array
 * @param array $thespec [<colname>] - Set for each column, as named in the database query used
 * @param array $thespec [<colname>] ['headname'] (opt) - Name to display on the header for the column.  If not there, will use <colname>
 * @param array $thespec [<colname>] [key] - If set (1), this will be used as a key field for record operations
 * @param array $thespec [<colname>] [show] - If set (1), this column will show in displays.  If 0 or unset, column will not appear
 * @param array $thespec [<colname>] [select] - How to display editable boxes: {T = Text Input, C = Check box (0 or 1), D = drop down) 
 * @param array $thespec [<colname>] [list] = array of values (showvalue, keyvalue) to use in drop-down.  Array is of form:  valueslist [<index integer>][<keyvariablename>][<showvariablename>] Where defaults are keyvariablename = 'keyvalue' showvariable='showvalue' unless re-assigned
 * @param array $thespec [<colname>] [showvalue] - (opt)  Re-assignes the <keyvariablename> to the string specified, instead of 'keyvalue'
 * @param array $thespec [<colname>] [keyvalue] - (opt) Re-assignes the <showvariablename> to the string specified, instead of 'showvalue'
 * @param array $thespec [<colname>] [sort] - Specifies if row header will sort or not, {true, false, (or undefined)}
 * @param array $thespec [<colname>] [filter] - Specifies if row is filtered. if set= 'keyword', then will  bet textbox for LIKE '%keyword%' search.  If set (= 1, but not specific setting ), then will filter using spec from edit criteria ([select],[list],)
 */		
	function set_fieldspec($thespec){
		$this->fieldspec = $thespec;
	}	
	
	/**
	 *  clear_keys_strings Method will clear keys array.  Placehoder, to be expanded
	 */
	function clear_keys_strings(){}
	/**
	 *  make_key_string Method will set keys array.  Empty, Placehoder, to be expanded
	 */
	 
	function make_key_string($col_name, $currentvalue){}
	

	/**
	* function to make start of row.  Override / extend in child classes
	* @param int $row_num This is the row number of the dataset
	* @param string $key_string This is the string of key values, defined by make_key_string function
	* @return string Row begining 
	*/
	function make_row_start($row_num){
		if($this->row_status == 'even'){
			$this->row_status = 'odd';
			$rowstart .= "<tr class=\"$this->table_odd_class\">";			
		}
		else{
			$this->row_status = 'even';
			$rowstart .= "<tr class=\"$this->table_odd_class\">";			
		}
		return $rowstart;
	}
	
	/**
	*  make_row_end Method will set create table row end.  returns end tag, to be expanded
	* in derived classes.
	* @param int $row_num Current row number
	*/
	function make_row_end($row_num){
		return "</tr>\n";	
	}
	
	/**
	 *  make_rowval Method will set create table row value.  Returns value in td.
	 * Expanded in derived classes.
	 * @param string $currentvalue Value to show
	 * @param string $col_name NAme of column to show
	 * @param string $current_text Starting text, not used, expend in derived clases
	 */	
	function make_rowval($currentvalue, $col_name, $current_text){
		return "<td>$currentvalue</td>";
	}
	
	/**
	 *  on_start_row Method will execute when row is started.  Placehoder, to be expanded
	 */	
	function on_start_row($row_num){
	}

	/**
	 * maketable will return the table text from the class, for use as the body of a table
	 * @return string
	 */
	function maketable(){
		$table_text = '';
		if(!empty($this->valuesarray)){
		foreach($this->valuesarray as $row_num => $currentrow) {	
			$this->on_start_row($row_num);
			$this->clear_keys_strings();  // Clear keys_string
			$row_text = '';
			foreach ($this->fieldspec as $col_name => $thefield){
				if(!empty($thefield['key'])){
					$currentvalue = $currentrow[$col_name];
					$this->keys[$col_name] = $currentrow[$col_name];
					$this->make_key_string($col_name, $currentvalue);
				}
			}
			foreach ($this->fieldspec as $col_name => $thefield){
				$currentvalue = $currentrow[$col_name];
                if(!$thefield['allowhtml']){
                    $currentvalue = htmlspecialchars($currentvalue,ENT_QUOTES);
                    }
				if(!empty($thefield['makefunction'])){
					$makefunction = $thefield['makefunction'];
					if(method_exists($this,$makefunction)){	
						$currentvalue = $this->$makefunction($currentvalue);
					}
				}
				if($this->show_all_rows or (!empty($thefield['show']))){
					$row_text .= $this->make_rowval($currentvalue, $col_name, $row_text);

				}// end show field
			} // end for each colucm in fieldspec
			$table_text .= $this->make_row_start($row_num) . $row_text . $this->make_row_end($row_num) ; 
		}// end for each row
		}// end is not empty valuesarray
		return $table_text;	
	}
    
	/**
	 * make_simple_heading will return a heading row of the column name, in order of fieldspecs presented in fields definition
	 * @return string
	 */
	function make_simple_heading(){
		foreach($this->fieldspec AS $col_name => $colspec){
			$headtext .= '<th>';
			(empty($colspec['headname']))?$headtext .= $col_name: $headtext .= $colspec['headname'] . "</th>\n";
			}
		return $headtext;
	}
	
} // End BasicTable Class

/**
 * csvTable is a class to output CSV from an array, usually returned form a Database
* @package CFtable
 * @author Chris Tyler
 * @version 1.0
 * @copyright 2007-2008 
 */
Class csvTable Extends BasicTable{

	// public vars
	var $msg_no_data = 'There is no data avalible for this csv';

	// private vars
	var $is_first_col = true;	
	
	// Functions
	
	function make_row_start($row_num){

	}
	
	/**
	 * make_row_end for CSV - insert line end, and reset first coumn bool
	 * @return string
	 */
	function make_row_end($row_num){
		$this->is_first_col = true; // carefull - that this is always run AFTER the rows are made!	
		return "\n";
	}
	
	/**
	 * make_rowval for CSV - escape the text properly, and only insert a , after the first value
	 * @return string
	 */	
	function make_rowval($currentvalue, $col_name, $current_text){
		if($this->is_first_col){
			$this->is_first_col = false;		
			return csvEscapeText($currentvalue);
			}
		else{
			return ',' . csvEscapeText($currentvalue);
			}
	}
	
	/**
	 * maketable will return the a heading line  (+ line return)  for the csv based on the fieldspec
	 * @return string
	 */		
	function make_heading(){
		if(!is_array($this->fieldspec)){
			return;
		}
		$after_first_col = false;
		foreach($this->fieldspec AS $col_name => $colspec){
			if($after_first_col){
				$headtext .= ',';
				}// if after_first_col = true, then add the comma.  if it is false, then don't.
			if(empty($colspec['headname'])){
				$headtext .= csvEscapeText($col_name);
				}
			else{
				$headname = $colspec['headname'];
				$headtext .= csvEscapeText($headname);
				}
			$after_first_col = true;
			}
		return $headtext . "\n";
	}
	
	/**
	 * export_to_csv will export the csv as a file to the browser.  It will use the current fieldspec and values array.
	 *@param string filename - the full filename (with extension) to use when returning the data.
	 *@param use_heading - Set to true to include a heading (from make_heading) as the first line of the file
	 * @return string
	 */
	function export_to_csv($filename, $use_heading = false){
		if($this->valuesarray){
			if($use_heading){
				$heading = $this->make_heading();
			}
			$csv_file_text = $heading . $this->maketable();
			
			header("Content-type: application/x-msdownload");
			header("Content-Disposition: attachment; filename=$filename");
			header("Pragma: no-cache");
			header("Expires: 0");
			echo $headers.$csv_file_text;
		}
		else{
			print $this->msg_no_data;
			}
	} // end export_to_csv

} // end Class csvTable



/**
 * CFtable is a class to simplfy creation of Databse CRUD (Create Remove Update Delete) Tables
* @package CFtable
 * @author Chris Tyler
 * @version 1.0
 * @CFtable is the base class for the an extensable HTML table class for array based data.  This provides data display, sorting, filtering, paging and heading functions
 * @copyright 2007-2008 
 */

class CFtable extends BasicTable{
	/**
	* @var string $table_name Defines name of the table (associated elements)
	*/	
	var $table_name = 'datatable';
	/**
	* @var string $table_text String of HTML for output as table
	*/	
	var $table_text;
	/**
	* @var string $selfpage The name of the page, usualy set by $_SERVER['SELF']
	*/
	var $selfpage;
	/**
	* @var array $gotopages The array of pages identified by the action, for the system to link to after ahe action
	* example:  $gotopages['edit'] = someformpage.php?mode=save
	*/
	var $gotopages = array();	
	/**
	* @var string $referstring :  A string to tag along to any pages that are called.  not tested
	*/
	var $referstring;	
	/**
	* @var string $liststartvar String used for list start variable
	*/	
	var $liststartvar = 'cfListStart'; 
	/**
	* @var string $liststartvar String used for list size variable
	*/		
	var $listsizevar = 'cfListSize';
	/**
	* @var string $top_dir Set absolute or relative reference to the base directory (for things in subdirectories)
	*/		
 	var $top_dir = '';

	/**
	* @var string $editvarprefix prefix for filtered variables (column name used)
	*/
	var $editvarprefix = '';


	// ----------------------------------- Variables changed by functions (might make these private someday...)
	
	/**
	* @var string $keys_url_edit Internal String used to show keys and values as row pamamerter
	*/	
    var $keys_url_edit;
	/**
	* @var string $keys_url_edit Internal String used to show keys and values as row pamamerter
	*/
    var $keys_url_act;

	/**
	* @var string $keys_uid keys as semicolon (;) delimited fields per row
	*/    
	var $keys_uid;
	/**
	* @var bool $show_filter_submit show sumbit filter (Default true)
	*/   	

	

// ---------------------------------FUNCTIONS---------------------------------
// ---------------------------------Public Functions--------------------------
	
/**
 * Constructor.  Override / extend
 * @param array $thespec - The fieldspec array
 * @param array $dataarray - The data array, data[<rownumber>][<column_name>]
 */		
	function CFtable( $thespec = '', $dataarray= '', $allowspec = 'rde' ){
		$this->fieldspec = $thespec;	
		$this->valuesarray = $dataarray;
		
		$this->selfpage = $_SERVER['PHP_SELF'];
		$this->ordervar = 'order';	
	}
   	/**
	 *  @param string $name Table Name
	 */	
	function set_name($name){
		$this->table_name = $name;	
	}
   	/**
	 * set_ordervar The variable to use for specifying column to order by  
	 * @param string $newordervar
	 */		
	function set_ordervar($newordervar){
		$this->ordervar = $newordervar;
	}	
	
	/**
	 * set_selfpage Set the page, usually set to $_SERVER['PHP_SELF']
	 * @param string $newpage The page 
	 */	
	function set_selfpage($newpage){
		$this->selfpage = $newpage;
	}	

	// Also in CF_Multiselect
   	/**
	 *  make_keyvariables  Method will output a comma delimited list of variables (columns) which are keys from fieldspec
	 */
	function make_keyvariables(){
		$aftercomma = '';
		foreach($this->fieldspec as $col_name => $colspec){
			if(!empty($this->fieldspec[$col_name]['key'] )){
				$keyvariables .= $aftercomma . $col_name;
				$aftercomma = ',';
			}
		}
		return $keyvariables;
	}
	

/**
 * function to make start of row.  Override / extend
 * @param int $row_num This is the row number of the dataset
 * @param string $key_string This is the string of key values, defined by make_key_string function
 * @return string Row begining 
 */
	function make_row_start($row_num){
		$rowstart = '';
		if($this->row_status == 'even'){
			$this->row_status = 'odd';
			$rowstart .= "<tr class=\"$this->table_even_class\">";			
		}
		else{
			$this->row_status = 'even';
			$rowstart .= "<tr class=\"$this->table_odd_class\">";			
		}
		//if($this->multiselect){
		//	$rowstart .= "<td>" . $this->make_multiselectCB() . "</td>";
		//}	
		return $rowstart;
	}

	function make_multiselectCB(){
		return "<input type='checkbox' name='" . $this->table_name . "_addto_ms' value='" . $this->keys_uid . "' >";
	
	}	
	
	function make_row_end($row_num){
		return "</tr>\n";
	}
	
	function make_rowval($value,$column){
		$row_text = '';
		$row_text .= "<td>";
		$row_text .= $value;
		$row_text .= "</td>";
		return $row_text;
			
	}

	function clear_keys_strings() { 
		$this->keys_url_edit = '';
        $this->keys_url_act = '';
		$this->keys_uid = '';
	}
	

	/**
	 *(Also in CF_Filter and CF_AddBox)
     * @make_element set will create a line of headed elements arranged in a table
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_set($elements, $headset, $bodyset){

        if(is_array($elements)){
        foreach ($elements AS $current){
            $headtext .= "<th>" . $current['head'] . "</th>\n";
            $bodytext .=  "<td>" . $current['body'] . "</td>\n";
        }
        }
        return "<tr $headset>$headtext </tr> <tr $bodyset>$bodytext</tr>";
    }

	
	/**
	 *
     * @make_form_set set will create a form out of the elements passed in
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */	
    function make_form_set($elements, $headset, $bodyset){

        if(is_array($elements)){
			foreach ($elements AS $col_name=>$current){
				$formtext .= "<div class='ms_$col_name' ><label for='$col_name'>" . $current['head'] . "</label>\n" . $current['body'] . "</div>\n";
			}
        }
        return "<fieldset>\n$formtext</fieldset>\n";
    }	
	
	/**
	 *
     * @make_element_div_set set will create a set of div for use as a form
     * from an input of an element_set array.  Each row has element_name and value
     * @param array elements An array with [body] and [head] parts where head
     * elements['head'] is the title and elements['body'] is what to put in the cell
     * @param string $headset Settings for heading elements (class name, etc)
     * @param string $bodyset Settings for body elemetns (class naem, etc)
	 *
	 */
    function make_element_div_set($elements, $headset, $bodyset){
        foreach ($elements AS $name=>$current){
            $outtext .= "<div id='entry_$name' class='entries'>\n";
            foreach($current AS $key=>$value){
            
                $outtext .= "<div class='$key'>\n $value </div>\n";
                }
            $outtext .= "</div>\n";
         }
         
        return $outtext;
    }
	



	function get_edit_keys(){
	//for each column which is a key, get the key and set the keys variable array
	foreach ($this->fieldspec as $col_name => $colspec){
		if((!empty($colspec['key'])) AND (isset($_REQUEST[$col_name])) AND ($_REQUEST[$col_name] != '') ){
			$this->keys[$col_name] = $_REQUEST[$col_name];
			}
		}
		//print_r ($this->keys);
	}
	
	
	/**
	 * function to make key string.  Override / extend
	 * @param int $col_name This is the column name for the key value
	 * @param string $currentvalue This is the value of the key
	 * @param int $firstrow This is 1 for the first row, 0 afterwards for special punctuation
	 * @return string keystring
	 */	
    function make_key_string($col_name, $currentvalue, $firstrow = 0){
		if(empty($this->keys_uid)){
			$this->keys_uid =  "$currentvalue";
	
		}
		else{
			$this->keys_uid .=  ",$currentvalue";
		}
		$this->keys_hidden .= "<input type='hidden' name='$col_name' value='$currentvalue'>";
		//parent::make_key_string($col_name, $currentvalue);
		$this->keys_url_edit .= "&$this->editvarprefix" . "$col_name=$currentvalue";
        $this->keys_url_act .= "&$col_name=$currentvalue";
		
	}
	
	function make_keys_clause(){
		foreach($this->fieldspec as $col_name => $colspec){
			if(!empty($this->fieldspec[$col_name]['key'] )){
				if(isset($this->keys[$col_name])){ // check if defined and exists in keys array
					$outtext .= " AND $col_name = '" . $this->keys[$col_name] . "' ";
				}else{
					$outtext .= " AND 0 "; // Not all specified keys were defined, so return 0 to kill any results form query
				}
			}
		}
		return $outtext ;
	}
	
	function make_keys_hidden(){
		foreach($this->fieldspec as $col_name => $colspec){
			if(!empty($colspec['key'] )){
				if(isset($this->keys[$col_name])){ // check if defined and exists in keys array
					$outtext .= " <input type='hidden' name='$col_name' value='" . $this->keys[$col_name] . "'> ";
				}
			}
		}
		return $outtext;
	}
		
	
}


class CF_EDpage_table extends CFtable{

	var $actionpage;		
	var $editpage;
	var $ordervar = 'order';
    var $edit_link_text = 'Edit';
    var $delete_link_text = 'Delete';
    var $edit_prefix;

	
	function set_actionpage($pagename){
		$this->actionpage = $pagename;
	}	
	
	function set_editpage($pagename){
		$this->editpage = $pagename;
	}

	// Overrides and Extension mehods	
	function make_row_end($row_num){
		$endstring .= "</tr>\n";
		return $endstring;
	}

	function make_edit($value){
			return "<a href='$this->editpage?mode=Edit$this->keys_url_edit$this->referstring'>$this->edit_link_text</a>";
			
	}

	function make_delete($value){
			return "<a href='$this->actionpage?mode=Delete$this->keys_url_act$this->referstring'>$this->delete_link_text</a>";	
	}

	// mostly here just to save the code...
		function make_PopUpEdit($keyvalue){
			return("<a href=\"javascript:makePopUpEdit('customers_dedit.php','customerNumber=" . $keyvalue . "','dynamic' )\">Click Here</a>");
		}	
	
}

class CF_InlineEDTable extends CF_EDpage_table {
	

	var $keys_hidden; // keys as hidden fields in form
	var $actionpage; // page form will go to for edit and delete actions
	var $ordervar = 'order';  // set this variable for order when re-ording this query with the headers
	var $oldvalvar = 'OV_';  // This is the prefix for original value in a form

	var $allow_save = true;
	
    var $mode_save = 'Save';
    var $mode_delete = 'Delete';
    var $mode_duplicate = 'Copy';
	var $mode_edit = 'Edit';


	// New Methods
	function set_actionpage($pagename){
		$this->actionpage = $pagename;
	}	

	
	function clear_keys_strings() { 
		$this->keys_uid = '';
		$this->keys_hidden = '';
		parent::clear_keys_strings();
	}
	
	function make_lookup_value($column,$value){
        $showkey = 'showvalue';
        $valuekey = 'keyvalue';
    	$valuesarray = $this->fieldspec[$column]['list'];
        if(isset($this->fieldspec[$column]['showvalue'])){
            $showkey = $this->fieldspec[$column]['showvalue'];
        }
        if(isset($this->fieldspec[$column]['keyvalue'])){
            $valuekey = $this->fieldspec[$column]['keyvalue'];
        }

        foreach ($valuesarray as $lookup_pair) {
            if($lookup_pair[$valuekey] == $value){
                return $lookup_pair[$showkey];
            }
         }
         return $value;

    }
	
	function make_rowval($value,$column){
		
		$row_text .= "<td>";
        $row_text .= $this->make_valuebox($value,$column);
		$row_text .= "</td>";
		return $row_text;
			
	}

   function make_valuebox($value,$column){
      	if($this->fieldspec[$column]['show'] == 'E'){
            $chgname = "validateData" . "('$column', '$this->keys_uid', '$column" . "_" . "$this->keys_uid')";
            $row_text .= MakeEditField($this->fieldspec[$column],$column,$value,$chgname,$column . '_' . $this->keys_uid,false,$this->top_dir);
			$ov_name = $this->oldvalvar . $column;
			$row_text .= "<input type='hidden' name='$ov_name' value='$value' id='$ov_name" . "_$this->keys_uid'>";
		}  else if($this->fieldspec[$column]['show'] == 'L') {
            $row_text .= $this->make_lookup_value($column,$value);
        }else {
            $row_text .= "$value";
		}
		return $row_text;
    }


 /**
 * function to make start of row, making each row a form for editing
 * @param int $row_num This is the row number of the dataset
 * @param string $key_string This is the string of key values, defined by make_key_string function
 * @return string Row begining 
 */
	function make_row_start($row_num){
		$formname = $this->tablename . '_' . $this->keys_uid;
		$rowstart .= "<form action='$this->actionpage' name='$formname' method='GET'>";

		$rowstart .= $this->keys_hidden;
		
		if($this->row_status == 'even'){
			$this->row_status = 'odd';
			$rowstart .= "<tr class=\"$this->table_even_class\">";			
		}
		else{
			$this->row_status = 'even';
			$rowstart .= "<tr class=\"$this->table_odd_class\">";			
		}

		return $rowstart;
	}


	function make_row_end($row_num){
		if($this->allow_save){
			$rowend .= "<td class='actsave'><input type='submit' name='mode' value='" . $this->mode_save . "'></td>";
		}
		$rowend .= "</tr></form>\n";
		
		return $rowend;
	}
	
}// end Class CF_InlineEDTable

class CF_AJTable extends CF_InlineEDTable{
   	/**
	 *  @var string $keyslist comma delimited list of key variables
	 */		
	var $keyslist;
   	/**
	 *  Form keylist of key columns
	 */		
	function form_keyslist(){
		$addcomma = '';
		$list = '';
		foreach($this->fieldspec AS $col_name=>$col_spec){
			if(!empty($col_spec['key'])){
				$list .= $addcomma . $col_name;
				$addcomma = ',';
				}
		}
		$this->keyslist = $list;
	}
		
	function make_rowval($value,$column){
		
		$chgname =  $this->tablename . ".syncVal" . "('$column', '$this->keys_uid', '$column" . "_" . "$this->keys_uid')";
		$row_text .= "<td>";
        $row_text .= $this->make_valuebox($value,$column);
        $row_text .= "</td>";
		return $row_text;			
	}

    function make_valuebox($value,$column){
      	if($this->fieldspec[$column]['show'] == 'E'){
            $chgname =  $this->tablename . ".syncVal" . "('$column', '$this->keys_uid', '$column" . "_" . "$this->keys_uid')";
            $row_text .= MakeEditField($this->fieldspec[$column],$column,$value,$chgname,$column . '_' . $this->keys_uid,false,$this->top_dir);
			$ov_name = $this->oldvalvar . $column;
			$row_text .= "<input type='hidden' name='$ov_name' value='$value' id='$ov_name" . "_$this->keys_uid'>";
		}  else if($this->fieldspec[$column]['show'] == 'L') {
            $row_text .= $this->make_lookup_value($column,$value);
        }else {
            $row_text .= "$value";
		}
		return $row_text;
    }

	function make_row_end($row_num){
		$rowend .= "</tr></form>\n";
		return $rowend;
	}
	
	function make_supportJS(){
		$this->form_keyslist();
		$jstext = "
	//------" .  $this->tablename  . " support Javascript from make_supportJS()
	//----- AJAX dataSync settings 
	// support functions in CF_ajtable.js
	$this->tablename = new CF_ajtable();
	$this->tablename.url_syncval = \"". $this->tablename . "_proc.php\";
	$this->tablename.keyvariables = '" . $this->keyslist . "';
	$this->tablename.tablename = '" . $this->tablename . "'	;
	// End from make_supportJS
		";
		return $jstext;
	}

}


